package com.gdk.ksoft.ui.reporters;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.gdk.ksoft.R;
import com.gdk.ksoft.assembly.dbhelper.FreporterDbHelper;
import com.gdk.ksoft.ui.reporters.adapters.OrderStatementAdapter;
import com.gdk.ksoft.assembly.utils.extensions.FormatUtils;
import com.gdk.ksoft.assembly.utils.system.SQLiteUtils;
import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.db.table.DbModel;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.lidroid.xutils.view.annotation.event.OnItemClick;

import java.util.HashMap;
import java.util.List;

/**
 * 销售日报表查询
 */
public class OrderSaleActivity extends Activity {

    @ViewInject(R.id.porderSale_txtTitle)
    private TextView txtTitle;//标题文字
    @ViewInject(R.id.porderSale_lstOrder)
    private ListView lstOrder;//销售单列表
    @ViewInject(R.id.porderSale_txtTotalMoney)
    private TextView txtTotalMoney;//总金额
    @ViewInject(R.id.porderSale_txtTotalPiece)
    private TextView txtTotalPiece;//总张数
    @ViewInject(R.id.porderSale_txtTotalCount)
    private TextView txtTotalCount;//总数量
    @ViewInject(R.id.porderSale_txtNoUploadCount)
    private TextView txtNoUploadCount;//未上传数量
    @ViewInject(R.id.porderSale_txtMoneyInfo)
    private TextView txtMoneyInfo;//现金信息
    @ViewInject(R.id.porderSale_txtBankInfo)
    private TextView txtBankInfo;//银行信息
    @ViewInject(R.id.porderSale_txtMalingInfo)
    private TextView txtMalingInfo;//抹零信息
    @ViewInject(R.id.porderSale_txtDebtInfo)
    private TextView txtDebtInfo;//欠款信息
    private DbUtils dbUtils;//数据库操作对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_sale);
        ViewUtils.inject(this);
        dbUtils = SQLiteUtils.getInstance(this).getDbUtils();
        Intent intent = getIntent();
        if (intent != null) {
            String findDate = intent.getStringExtra("dateToFind");//获得查询的日期
            txtTitle.setText(findDate + "销售日报");
            initActivity(this,dbUtils, findDate);//初始化页面数据
        }
    }

    /**
     * 列表项点击事件
     *
     * @param av
     * @param view
     * @param position
     * @param itemId
     */
    @OnItemClick(value = {R.id.porderSale_lstOrder})
    public void onItemClick(AdapterView<?> av, View view, int position, long itemId) {
        HashMap<String, Object> item = (HashMap<String, Object>) lstOrder.getItemAtPosition(position);
        Intent intent = new Intent(this, OrderSaleDetailActivity.class);
        intent.putExtra("orderNo", item.get("xdjh").toString());//写入单据号
        startActivity(intent);
        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
    }

    /**
     * 初始化页面数据
     * @param context
     * @param date
     */
    public void initActivity(final Context context,final DbUtils dbUtils, final String date) {
        new AsyncTask<Void, Void, Void>() {

            private ProgressDialog dialog = null;
            private String errorMessage = null;
            private List<HashMap<String, Object>> list = null;//销售单列表
            private HashMap<String,Object> totalInfo = null;//统计信息
            private DbModel accountInfo = null;//统计信息

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                dialog = ProgressDialog.show(context, "", "正在查询中...");
            }

            @Override
            protected Void doInBackground(Void... params) {
                try {
                    list = FreporterDbHelper.findAllOrderStatments(dbUtils, date, 0);
                    totalInfo = FreporterDbHelper.findAllOrderTotalInfo(dbUtils,date,0);
                    accountInfo = FreporterDbHelper.makeAccountByDate(dbUtils,date);
                } catch (Exception e) {
                    errorMessage = e.getMessage();
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                super.onPostExecute(aVoid);
                if (dialog != null && dialog.isShowing()) dialog.dismiss();
                if (errorMessage != null) {
                    Toast.makeText(context, "查询出错了!原因：" + errorMessage, Toast.LENGTH_SHORT).show();
                    return;
                }
                if(accountInfo!=null){//账户查询统计
                    txtBankInfo.setText(FormatUtils.exactNumber(accountInfo.getDouble("tyh"),"0.00")+"元  "+accountInfo.getInt("ccl")+"张");
                    txtDebtInfo.setText(FormatUtils.exactNumber(accountInfo.getDouble("tqk"),"0.00")+"元  "+accountInfo.getInt("bbl")+"张");
                    txtMoneyInfo.setText(FormatUtils.exactNumber(accountInfo.getDouble("txj"),"0.00")+"元  "+accountInfo.getInt("aal")+"张");
                    txtMalingInfo.setText(FormatUtils.exactNumber(accountInfo.getDouble("tml"),"0.00")+"元  "+accountInfo.getInt("ddl")+"张");
                }
                if(totalInfo!=null){//显示统计信息
                    txtNoUploadCount.setText(totalInfo.get("noupload").toString());
                    txtTotalCount.setText(totalInfo.get("totalXCount").toString());
                    txtTotalMoney.setText(FormatUtils.exactNumber(totalInfo.get("totalMoney"), "0.00")+"元");
                    txtTotalPiece.setText(totalInfo.get("totalCount").toString()+"张");
                }
                lstOrder.setAdapter(new OrderStatementAdapter(context, list));
            }

        }.execute();
    }

    /**
     * 页面返回
     */
    public void goBack() {
        this.finish();
        overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
    }

    @OnClick(value = {R.id.porderSale_imgBack})
    public void OnBackClick(View view) {
        goBack();//页面返回
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
            goBack();//页面返回
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
